package com.aaa.system.service.impl;

import com.aaa.system.dao.ChangePasswordDao;
import com.aaa.system.service.ChangePasswordService;
import com.aaa.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Author 崔灿
 * @Date 2023/03/02 15:24
 * @ClassName ChangePasswordServiceImpl
 * @Description
 * @Version 1.0
 */

@Service
public class ChangePasswordServiceImpl implements ChangePasswordService {

    @Autowired
    private ChangePasswordDao changePasswordDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result ChangePassword(String passold) {
        String usernamee = redisTemplate.opsForValue().get("usernamee");
        String oldPw = changePasswordDao.selectOldPw(usernamee);
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        if (passwordEncoder.matches(passold,oldPw)){
            return new Result(2000,"该密码可用");
        }
        return new Result(5000,"该密码不可用");
    }

    @Override
    public Result ChangePassword1(String  pass) {
        String username = redisTemplate.opsForValue().get("usernamee");
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(pass);

        Integer integer = changePasswordDao.ChangePassword1(username, encode);
        return new Result(2000,"更新成功",integer);
    }
}
